package com.nl.controller.web.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nl.controller.common.annotation.SystemServiceLog;
import com.nl.controller.common.exception.SmsException;
import com.nl.controller.common.service.CommonService;
import com.nl.controller.common.util.AES;
import com.nl.controller.web.bean.BlackUser;
import com.nl.controller.web.global.GlobalConst;
import com.nl.controller.web.mapper.BlackUserMapper;
import com.nl.controller.web.mapper.UtilMapper;
import com.nl.controller.web.util.FileUtil;

import lombok.extern.slf4j.Slf4j;



@Slf4j
@Service
public class BlackUserService {

    @Autowired
    private BlackUserMapper blackUserMapper;
    @Autowired
    private CommonService commonService;
    @Autowired
    private UtilMapper utilMapper;
    


    @SystemServiceLog(value = false, description = "黑名单添加")
    public int addBlackUser(String phoneNumber, String cityId,  String mgrId, String blackLevel,String enterTime) {
        int result = 0;
        int[] cityIds = {11,12,13,14,15,16,17,18,19,20,21,22,23};
        String[] arr = null;
        boolean status = mgrId.contains("+");
        String[] phoneNumberList = phoneNumber.split(",");
        if(status){
        	arr = mgrId.split("\\+");
        	if ("25".equals(cityId)) {
                for (int i : cityIds) {
                	for(int j=0;j<arr.length;j++){
	                	  for(int k=0;k<phoneNumberList.length;k++){
	                		  BlackUser blackUser = new BlackUser();
	                          blackUser.setCityId(i+"");
	                          blackUser.setPhoneNumber(phoneNumberList[k]);
	                          blackUser.setBlackLevel(blackLevel);
	                          blackUser.setMgrId(arr[j]);
	                          //blackUser.setCountyId(countyId);
	                          blackUser.setEnterTime(enterTime);
	                          result = add(blackUser);
	          			  }	  
                    }
                }
            }else {
            	for(int j=0;j<arr.length;j++){
            		for(int k=0;k<phoneNumberList.length;k++){
            			BlackUser blackUser = new BlackUser();
                        blackUser.setCityId(cityId);
                        blackUser.setPhoneNumber(phoneNumberList[k]);
                        blackUser.setBlackLevel(blackLevel);
                        blackUser.setMgrId(arr[j]);
                        //blackUser.setCountyId(countyId);
                        blackUser.setEnterTime(enterTime);
                        result = add(blackUser);
            		}
            	}
            }
        }else if(!status&&!"".equals(mgrId)&&null!=mgrId){
        	if ("25".equals(cityId)) {
                for (int i : cityIds) {
                	for(int k=0;k<phoneNumberList.length;k++){
                		  BlackUser blackUser = new BlackUser();
                          blackUser.setCityId(i+"");
                          blackUser.setPhoneNumber(phoneNumberList[k]);
                          blackUser.setBlackLevel(blackLevel);
                          blackUser.setMgrId(mgrId);
                          //blackUser.setCountyId(countyId);
                          blackUser.setEnterTime(enterTime);
                          result = add(blackUser);
                	}
                }
            }else {
            	for(int k=0;k<phoneNumberList.length;k++){
            		BlackUser blackUser = new BlackUser();
                    blackUser.setCityId(cityId);
                    blackUser.setPhoneNumber(phoneNumberList[k]);
                    blackUser.setBlackLevel(blackLevel);
                    blackUser.setMgrId(mgrId);
                    //blackUser.setCountyId(countyId);
                    blackUser.setEnterTime(enterTime);
                    result = add(blackUser);
            	}
            }
        }
        return result;
    }

    @SystemServiceLog(value = false, description = "黑名单查询")
    public PageInfo<BlackUser> find(int page, int limit, String cityId, String phoneNumber,String countyId,String mgrId,String blackLevel,String mgrName) {
        PageHelper.startPage(page, limit);
        List<BlackUser> list = new ArrayList<>();
        try {
        	if(null!=phoneNumber&&!"".equals(phoneNumber)){
        		phoneNumber = AES.encrypt(phoneNumber, GlobalConst.SKEY);
        	}
            list = blackUserMapper.getDataList(cityId, phoneNumber,countyId,mgrId,blackLevel,mgrName);
            for (BlackUser blackUser : list) {
                String decodePhoneNumber = AES.decrypt(blackUser.getPhoneNumber(), GlobalConst.SKEY);
                blackUser.setDecodePhoneNumber(decodePhoneNumber);
                if(null==blackUser.getCityId()||"".equals(blackUser.getCityId())||"25".equals(blackUser.getCityId())){
                	blackUser.setCityName("-");
                }else{
                	blackUser.setCityName(blackUser.getCityName()+"市");
                }
                if(null==blackUser.getCountyId()||"".equals(blackUser.getCountyId())){
                	blackUser.setCountyName("-");
                }else{
                	blackUser.setCountyName(utilMapper.getCountyName(blackUser.getCountyId()));
                }
                blackUser.setMgrName(StringUtils.isEmpty(blackUser.getMgrName())?"-":blackUser.getMgrName());
                blackUser.setBelong("江苏省/"+blackUser.getCityName2()+"/"+blackUser.getCountyName()+"/"+blackUser.getMgrName());
            }
            commonService.createSmsOperLog("4", "黑名单查询", "0", "黑名单管理");

        } catch (Exception e) {
            e.printStackTrace();
            commonService.createSmsOperLog("4", "黑名单查询", "1", "黑名单管理");
            log.error("黑名单查询");
            throw new SmsException("黑名单查询异常：" + cityId);
        }
        PageInfo<BlackUser> pageInfo = new PageInfo<>(list);
        return pageInfo;

    }

    @SystemServiceLog(value = false, description = "黑名单删除")
    public int deleteBlackUser(String phoneNumber,String mgrId) {
        int result;
        try {
            result = blackUserMapper.deleteUser(phoneNumber,mgrId);
            commonService.createSmsOperLog("5", "黑名单删除", "0", "黑名单管理");
        } catch (Exception e) {
            e.printStackTrace();
            commonService.createSmsOperLog("5", "黑名单删除", "1", "黑名单管理");
            log.error("黑名单删除出现异常：" + phoneNumber+","+mgrId);
            throw new SmsException("黑名单删除出现异常：" + phoneNumber+","+mgrId);
        }
        return result;
    }
    
    private int add(BlackUser blackUser) {
        
        int result;
        try {
            String phoneNumber = AES.encrypt(blackUser.getPhoneNumber(), GlobalConst.SKEY);
            blackUser.setPhoneNumber(phoneNumber);
            result = blackUserMapper.addBlackUser(blackUser);
            commonService.createSmsOperLog("2", "黑名单添加", "0", "黑名单管理");
        } catch (Exception e) {
            e.printStackTrace();
            commonService.createSmsOperLog("2", "黑名单添加", "1", "黑名单管理");
            log.error("黑名单添加出现异常：" + blackUser.getPhoneNumber());
            throw new SmsException("黑名单添加出现异常：" + blackUser.getPhoneNumber());
        }
        return result;
    }
    @SystemServiceLog(value = false, description = "处理文件上传")
    public List<String> readCsv(String path) throws IOException {
        List list;
        list = FileUtil.readCSV(path);
        List<String> phoneList=new ArrayList<String>();
        for (int i = 0; i < list.size(); i++) {
        	phoneList.add((String) list.get(i));
        }
        return phoneList;
    }
}
